package com.zenith.user.controller;

import com.zenith.common.annotation.request.SecurePostMapping;
import com.zenith.common.result.Result;
import com.zenith.common.result.PageResult;
import com.zenith.user.po.domain.UserRole;
import com.zenith.user.po.dto.UserRoleDTO;
import com.zenith.user.service.UserRoleService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

/**
 * 用户角色关联管理
 *
 * @author makejava
 * @since 2025-08-15
 */
@Slf4j
@RestController
@RequestMapping("/api/user/userRole")
@Tag(name = "用户角色关联管理")
public class UserRoleController {

    @Autowired
    private UserRoleService userRoleService;

    @SecurePostMapping(value = "/addOrUpdateUserRole", roles = {"ADMIN"}, permission = "userRole:write")
    @Operation(summary = "添加或修改用户角色关联")
    public Result<Void> saveOrUpdate(@RequestBody UserRole entity) {
        log.info("添加或修改用户角色关联: {}", entity);
        userRoleService.saveOrUpdate(entity);
        return Result.success();
    }

    @SecurePostMapping(value = "/deleteUserRoles", roles = {"ADMIN"}, permission = "userRole:delete")
    @Operation(summary = "批量删除用户角色关联")
    public Result<Void> deleteUserRoles(@RequestBody List<String> ids) {
        log.info("批量删除用户角色关联: {}", ids);
        userRoleService.removeByIds(ids);
        return Result.success();
    }

    @SecurePostMapping(value = "/getUserRoleById", roles = {"ADMIN", "USER"}, permission = "userRole:read")
    @Operation(summary = "根据ID查询用户角色关联")
    public Result<UserRole> getUserRoleById(@RequestParam String id) {
        log.info("根据ID查询用户角色关联: {}", id);
        return Result.success(userRoleService.getById(id));
    }

    @SecurePostMapping(value = "/getUserRoleList", roles = {"ADMIN"}, permission = "userRole:read")
    @Operation(summary = "获取用户角色关联列表")
    public Result<List<UserRole>> getUserRoleList() {
        log.info("获取用户角色关联列表");
        return Result.success(userRoleService.list());
    }

    @SecurePostMapping(value = "/getUserRolePage", roles = {"ADMIN"}, permission = "userRole:read")
    @Operation(summary = "分页查询用户角色关联")
    public Result<PageResult<UserRole>> getUserRolePage(@RequestBody UserRoleDTO query) {
        log.info("分页查询用户角色关联: {}", query);
        return Result.success(userRoleService.getUserRolePage(query));
    }

    @SecurePostMapping(value = "/getUserRoleIds", roles = {"ADMIN"}, permission = "userRole:read")
    @Operation(summary = "获取用户角色ID列表")
    public Result<Set<String>> getUserRoleIds(@RequestParam("userId") Long userId) {
        log.info("获取用户角色ID列表: {}", userId);
        return Result.success(userRoleService.getUserRoleIds(userId));
    }

    @SecurePostMapping(value = "/getUserPermissionIds", roles = {"ADMIN"}, permission = "userRole:read")
    @Operation(summary = "获取用户权限ID列表")
    public Result<Set<String>> getUserPermissionIds(@RequestParam("userId") Long userId) {
        log.info("获取用户权限ID列表: {}", userId);
        return Result.success(userRoleService.getUserPermissionIds(userId));
    }
}